package com.pan.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步调用api专用线程池配置
 *
 * @author cpl_wwl
 */
@Configuration
@EnableAsync
@Slf4j
public class AsyncApiThreadPoolConfiguration {

    @Bean("apiAsyncCall")
    public Executor apiAsyncCall() {
        log.info("ApiAsyncCall thread pool init");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        // 需配合微服务的hystrix的coreSize配置一并配置, 先统一调整为50, 待后续整合微服务配置中心后统一配置
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(10);
        executor.setThreadNamePrefix("apiAsyncCall@");
        // 线程池满后直接在主线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
